﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace nSL.Libs
{
    public class FileHelper
    {
        public static bool ByteArrayToFile(string _FileName, byte[] _ByteArray)
        {
            try
            {
                // Open file for reading
                System.IO.FileStream _FileStream =
                   new System.IO.FileStream(_FileName, System.IO.FileMode.Create,
                                            System.IO.FileAccess.Write);
                // Writes a block of bytes to this stream using data from
                // a byte array.
                _FileStream.Write(_ByteArray, 0, _ByteArray.Length);

                // close file stream
                _FileStream.Close();

                return true;
            }
            catch (Exception _Exception)
            {
                // Error
                Console.WriteLine("Exception caught in process: {0}",
                                  _Exception.ToString());
            }

            // error occured, return false
            return false;
        }

        public static void pdfFromURL(string URL,string targetSave)
        {
            var wc = new WebClient();
            Stream stream = wc.OpenRead(URL);
            byte[] bStream = StreamHelper.ReadToEnd(stream);
            ByteArrayToFile(targetSave, bStream);
            wc.Dispose();
        }

        public static bool Downloader(string URL, string targetFile)
        {
            try
            {
                var wc = new WebClient();
                Stream stream = wc.OpenRead(URL);
                byte[] bStream = StreamHelper.ReadToEnd(stream);
                ByteArrayToFile(targetFile, bStream);
                wc.Dispose();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static void toStrream()
        {

        }
    }
}
